package cn.wit.summit.chwan.net;

import android.os.AsyncTask;

import com.google.gson.Gson;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import cn.wit.summit.chwan.models.request.GetGameCommentRequest;
import cn.wit.summit.chwan.models.response.CommentDetailBean;

/**
 * Created by Alan on 2018/6/3.
 */

public class CommentAsyncTask extends AsyncTask<String,Void,CommentDetailBean> {

    private OnResultListener onResultListener;

    public void setOnResultListener(OnResultListener onResultListener) {
        this.onResultListener = onResultListener;
    }

    @Override
    protected CommentDetailBean doInBackground(String... strings) {
        CommentDetailBean commentDetailBean = null;
        String commentRequest = strings[0];

        try {
            URL url=new URL(RpcConstant.getCommentList); //获取传来的URL
            HttpURLConnection conn=(HttpURLConnection) url.openConnection();    //获取HttpURLConnection对象

            conn.setRequestMethod("POST");  //设置发送请求的方法是post
            conn.setDoOutput(true);         //设置允许输出（post时必须设置，get是不用设置）
            conn.setConnectTimeout(5000);   //设置连接超时的时间限制
            conn.setReadTimeout(5000);      //设置读取时间限制
            conn.setRequestProperty("Charset", "UTF-8");            //设置文字字符集
            conn.setRequestProperty("Connection", "Keep-Alive");    //设置维持长连接

            OutputStream os=conn.getOutputStream(); //获取URLConnetction对应的输出流，用于向URLConnetction发送请求参数
            PrintWriter pw=new PrintWriter(os); //创建文本输出流
            pw.print(commentRequest); //发送请求的参数
            pw.flush();
            pw.close();

            if(conn.getResponseCode()==200) //对响应码进行判断，是否连接成功
            {
                InputStream is=conn.getInputStream();   //获取URLConnetction对应的输出流,用于获取URLConnection响应的内容
                byte[] bt=new byte[1024];
                StringBuffer sb=new StringBuffer();
                int readLine=0;
                while(-1 != (readLine=is.read(bt)))
                {
                    sb.append(new String(bt, 0, readLine));     ////先把字节转换成字符，再拼接成字符串        参数：1.byte数组 2.开始转换字节的下标 3.字节数组的长度
                }

                Gson g = new Gson();
                commentDetailBean = g.fromJson(sb.toString(), CommentDetailBean.class);
//                One one=g.fromJson(sb.toString(), One.class);   //解析JSON数据
//                Log.e("TAG", "解析的数据是："+one.toString()+one.result.data.toString());
                return commentDetailBean;
            }else
            {
                throw new RuntimeException("请求url失败");
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return commentDetailBean;

    }

    @Override
    protected void onPostExecute(CommentDetailBean bean) {
        super.onPostExecute(bean);
        if(bean!=null){
            onResultListener.onSuccess(bean);
        }else {
            onResultListener.onFailed();
        }
    }

    public interface OnResultListener{
        void onSuccess(CommentDetailBean bean);
        void onFailed();
    }
}
